<script lang="ts" setup>
  import { ref, onMounted, onUnmounted } from 'vue';
  import * as echarts from 'echarts';
  const props = defineProps({
    data1: { type: Object as () => any, required: true },
    data2: { type: Object as () => any, default: [] },
    data3: { type: Object as () => any, default: [] },
    datax: { type: Object as () => any, required: true },
    dataName: { type: Object as () => any, default: [] },
    markLine: { type: Number, default: 0 },
    title: { type: String, default: '' },
  });

  const chartRef = ref<HTMLElement | null>(null);
  let chartInstance: echarts.ECharts | null = null;

  const option = ref<any>({
    title: {
      text: props.title,
      left: 'center',
      top: '3%',
      textStyle: {
        fontSize: 14,
        fontWeight: 'bold',
      },
    },
    legend: {
      data: props.dataName,
      top: props.title == '' ? '0%' : '12%', // 图例位置
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true, // 让 grid 包含坐标轴文本
    },
    xAxis: {
      type: 'category',
      data: props.datax,
      axisLabel: { show: true, color: '#333', interval: 0 },
      axisLine: { show: false },
      axisTick: { show: false },
    },
    yAxis: {
      type: 'value',
      name: props.dataName[0] || '',
      splitLine: { show: false },
    },
    series: [
      {
        name: props.dataName[0],
        type: 'line',
        itemStyle: {
          color: '#5470c6',
        },
        data: props.data1,
        smooth: false,
      },
    ],
  });

  const initCharts = () => {
    try {
      option.value.series[0].data = props.data1 ?? [];
      if (props.data2.length > 0) {
        option.value.series.push({
          name: props.dataName[1],
          type: 'line',
          itemStyle: {
            color: '#fac858', // 使用橙色表示实际
          },
          data: props.data2,
          smooth: false,
          // label: {
          //   show: true,
          //   position: 'top',
          // },
        });
      }
      if (props.data3.length > 0) {
        option.value.series.push({
          name: props.dataName[2],
          type: 'line',
          data: props.data3,
          itemStyle: {
            color: '#91cc75',
          },
          smooth: false,
          // label: {
          //   show: true,
          //   position: 'top',
          // },
        });
      }

      if (props.markLine && props.markLine != 0) {
        const markLine = {
          symbol: 'none',
          data: [
            {
              yAxis: props.markLine,
              lineStyle: {
                color: 'red',
                width: 1,
              },
            },
          ],
        };
        option.value.series[0].markLine = markLine;
      }
      option.value.xAxis.data = props.datax ?? [];
      if (chartRef.value && !chartInstance) {
        chartInstance = echarts.init(chartRef.value);
        chartInstance?.setOption(option.value, true);
      }
    } catch (error) {
      console.error('图表初始化失败:', error);
    }
  };

  const updateChart = () => {
    option.value.series[0].data = props.data1 ?? [];
    if (props.data2.length > 0) {
      option.value.series.push({
        name: props.dataName[1],
        type: 'bar',
        itemStyle: {
          color: '#fac858', // 使用橙色表示实际
        },
        data: props.data2,
        smooth: false,
        // label: {
        //   show: true,
        //   position: 'top',
        // },
      });
    }
    if (props.data3.length > 0) {
      option.value.series.push({
        name: props.dataName[2],
        type: 'bar',
        data: props.data3,
        itemStyle: {
          color: '#91cc75',
        },
        smooth: false,
        // label: {
        //   show: true,
        //   position: 'top',
        // },
      });
    }
    option.value.xAxis.data = props.datax ?? [];
    chartInstance?.setOption(option.value, true);
    chartInstance?.resize();
  };

  onMounted(() => {
    // 确保在 DOM 加载完成后更新图表
    setTimeout(() => {
      initCharts();
    }, 100);

    // 添加窗口大小变化监听
    window.addEventListener('resize', () => chartInstance?.resize());
  });

  onUnmounted(() => {
    // 移除窗口大小变化监听
    window.removeEventListener('resize', updateChart);
    chartInstance?.dispose();
  });

  watch(
    () => [props.datax, props.data1],
    () => updateChart(),
    { deep: true }
  );

  defineExpose({
    updateChart, // 暴露方法到父组件
    chartRef,
  });
</script>
<template>
  <div ref="chartRef" class="chart-container"></div>
</template>

<style scoped>
  .chart-container {
    width: 100%;
    height: 100%;
  }
</style>
